use sqlx::SqlitePool;
use crate::{config, error::AppErr, utils::UninitCell};

static POOL: UninitCell<SqlitePool> = UninitCell::uninit();

pub mod proj;
pub mod firm_type;
pub mod firm_file;
pub mod device;


pub async fn init() -> Result<(), AppErr> {
    let url = format!("sqlite://{}/data.db?mode=rwc", config::DATA_DIR);
    let pool = SqlitePool::connect(&url).await?;
    unsafe { POOL.init_once(pool); }
    init_table().await?;
    Ok(())
}

async fn init_table() -> Result<(), AppErr> {
    proj::init().await?;
    firm_type::init().await?;
    firm_file::init().await?;
    device::init().await?;
    Ok(())
}

#[inline(always)]
pub fn get_pool() -> &'static SqlitePool {  
    &POOL
}






